﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RestaurateurConsole.Core
{
    public class SoldItemsHandler
    {
        DataEncapsulator Data;
        private SoldItemsHandler() { }
        public SoldItemsHandler(DataEncapsulator Data)
        {
            this.Data = Data;
        }

        public IEnumerable<SoldItem> SoldItems
        {
            get
            {
                return Data.DB.SoldItems.AsEnumerable();
            }
        }

        public void Remove(int ID)
        {
            var item = Data.DB.SoldItems.First(S => S.SoldItemsID == ID);
            Data.DB.SoldItems.DeleteOnSubmit(item);
            Data.DB.SubmitChanges();
        }

        public void RemoveDeferred(int ID)
        {
            var item = Data.DB.SoldItems.First(S => S.SoldItemsID == ID);
            Data.DB.SoldItems.DeleteOnSubmit(item);
        }

        public void Commit()
        {
            Data.DB.SubmitChanges();
        }

        public void Add(IEnumerable<SoldItem> soldItems)
        {
            Data.DB.SoldItems.InsertAllOnSubmit(soldItems);
            Data.DB.SubmitChanges();
        }

        public IEnumerable<SoldItem> Filter(int salesID)
        {
            return from i in Data.DB.SoldItems
                   where i.SalesID == salesID
                   select i;
        }
    }
}
